package com.ruoyi.goods.controller;

import com.aliyun.oss.OSS;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.douyin.utils.MessageUtils;
import com.ruoyi.goods.utils.AliOssConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/utils")
public class UtilsController {

    @Autowired
    private RedisCache redisCache;

    @GetMapping("/send/msg")
    public AjaxResult sendMsg(String mobile) {
        String randomNum = MessageUtils.randomNum();
        String uuid = UUID.randomUUID().toString();
        redisCache.setCacheObject("num:" + uuid, Integer.parseInt(randomNum), 5L, TimeUnit.MINUTES);
        MessageUtils.sendMessage(mobile, "#code#=" + randomNum, "29708");
        return AjaxResult.success(uuid);
    }

    @PostMapping("/upload/img")
    public AjaxResult sendMsg(MultipartFile multipartFile) {
        // if (multipartFile.getSize() > (1024 * 500)) return AjaxResult.error("上传不可以超过500k");
        OSS oss = AliOssConfig.getOss();
        String url = "";
        try {
            String originalFilename = System.currentTimeMillis() + multipartFile.getOriginalFilename();
            String format = new SimpleDateFormat("yyyy/MM/dd").format(new Date());
            String objectName = format + "/" + originalFilename;
            url = objectName;
            oss.putObject("i-shell", objectName, multipartFile.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            oss.shutdown();
        }
        return AjaxResult.success("https://i-shell.oss-cn-beijing.aliyuncs.com/" + url);
    }
}
